package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ck
 * @date 2021/10/18 13:15
 */
@RestController
@RequestMapping("/addressBook")
@Slf4j
@Api(tags = "地址簿相关接口")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    @PostMapping
    @ApiOperation("新增地址簿接口")
    @CacheEvict(value = "addressBookCache",allEntries = true)
    public R<AddressBook> save(@RequestBody AddressBook addressBook){
        addressBook.setUserId(BaseContext.get());
        addressBookService.save(addressBook);
        return R.success(addressBook);
    }

    @PutMapping("default")
    @ApiOperation("设置默认地址簿接口")
    @CacheEvict(value = "addressBookCache",allEntries = true)
    public R<AddressBook> setDefault(@RequestBody AddressBook addressBook){
        addressBookService.setDefault(addressBook);
        return R.success(addressBook);
    }

    @GetMapping("default")
    @ApiOperation("获取地址簿接口")
    @CacheEvict(value = "addressBookCache",key = "'shifou'")
    public R<AddressBook> getDefault(){
        R<AddressBook> r = addressBookService.getDefault();
        return r;
    }

    @GetMapping("/list")
    @ApiOperation("查看所有地址接口")
    @CacheEvict(value = "addressBookCache",key = "#addressBook.id+'_'+#addressBook.getPhone()")
    public R<List<AddressBook>> list(AddressBook addressBook){
        R<List<AddressBook>> listR = addressBookService.catList(addressBook);
        return listR;
    }
}
